﻿using System;
using UserInterface.Misc;

namespace UserInterface
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            string emoticonsFile = Server.MapPath("~/Emoticons.xml");
            MML.Manager.Initialize(emoticonsFile);
        }

        protected void Session_Start(object sender, EventArgs e)
        {
            if (!Context.Request.FilePath.StartsWith("/Install/"))
            {
                SessionManager.NewSession(UITools.GuestUserId);
            }
        }

        protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            if (!Context.Request.FilePath.StartsWith("/Install/"))
            {
                if (Context.Session != null)
                {
                    Session session = Session[SessionManager.SessionKey] as Session;
                    if (session != null)
                    {
                        session.LastActiveDateTime = DateTime.Now;
                        session.LocationUrl = Request.Url.ToString();
                    }
                }
            }
        }

        protected void Session_End(object sender, EventArgs e)
        {
            if (!Context.Request.FilePath.StartsWith("/Install/"))
            {
                SessionManager.Remove(Session[SessionManager.SessionKey] as Session);
            }
        }
    }
}